Skip to content

docs: a local's initial value can keep a constant out of its own register - #485

Merged
gynt merged 1 commit into
sourcehold:mainfrom
kotijekriv:fix/msvc-backend-initial-value
Nov 24, 2026
Merged

gynt merged 1 commit into
sourcehold:mainfrom
kotijekriv:fix/msvc-backend-initial-value

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

The section "A defect class: which constant gets the callee-saved register" of tools/compiler-internals/msvc-backend.md ends by saying that the allocator's choice of what to keep in a callee-saved register has no source handle. #484 found a source form that decides whether a constant gets a register of its own. This PR adds it to the end of that section as two paragraphs.

The case. In AlphaAndButtonSurface::openBuildingStatusMenuForBuildingID (0x00463310), the original pushes esi, which holds 1, as the second argument of extendResourceCountdownForPlayerBuildingsOfType (the push esi at 0x004633BD). The draft pushed the immediate 1, which left it one instruction off. The draft gave the local menuTab = 1 as its initial value, and in the second switch the barracks' arm was the one arm that relied on it. These all still gave push 1: the argument written as menuTab, TRUE, 1u or (dword)1, an int tab, and two definitions of it. Assigning the 1 in both arms of the owner test was worse (quick_diff 0.9929 against 0.9964). With no initial value and the 1 assigned in the barracks' arm, the push uses esi and the function is at 100% (#484).

What reg_scores.py shows. menuTab has the function's highest +0x0c key (175, and 176 after the change) and gets esi in both texts. After the change the probe lists one candidate more: an unnamed (temp) that also gets esi where menuTab is not live yet. By where it sits it is the constant 1. The probe cannot name it, and the paragraph says so.

What changes

  • A paragraph after the updateAIPlayerState one gives the case, with the numbers above.
  • A second paragraph gives the lever: drop the initial value and assign the constant only where the code relied on it, then compile once with /w14701 to see that every path still assigns the local (C4701 stays quiet). It says that the section's finding is about spellings of the same value, which this lever is not, and that the lever rests on one function so far.

Documentation only: no game source, header or build file changes, so no function's match percentage can move.

Other PRs. #481, #366 and #396 also change msvc-backend.md, in other places. git merge-tree merges each of them with this branch; #396 conflicts in cmake/compiler-flags-gl.txt, as it already does with main. #418 brings updateAIPlayerState itself to 100% with a source change (two more stores of 0), so the section's "no source handle" will need a correction once #418 is in. This PR leaves that sentence as it is.

🤖 Generated with Claude Code

…gister of its own

Two paragraphs in msvc-backend.md after "A defect class: which constant gets the callee-saved register", from
openBuildingStatusMenuForBuildingID (0x00463310, sourcehold#484). With menuTab initialised to 1, the original's push esi was
our push 1. With no initial value and the 1 assigned only in the one arm that relied on it, the probe lists one
candidate more, a temp that also gets esi where menuTab is not live, and the push uses it. The paragraphs say that
assigning it early was worse, how to check that every path still assigns the local (/w14701), how this differs from
the section's "no source handle" (it changes which candidates exist, not how a value is spelled), and that it rests
on one function so far.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gynt
gynt added this pull request to the merge queue Nov 24, 2026
Merged via the queue into sourcehold:main with commit 642f7ef Nov 24, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants